package com.school.service.Impl;

import com.school.common.ServiceResultEnum;
import com.school.controller.vo.IndexCarouselVO;
import com.school.entity.Carousel;
import com.school.mapper.CarouselMapper;
import com.school.service.CarouselService;
import com.school.util.BeanUtil;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CarouselServiceImpl implements CarouselService {

    @Resource
    CarouselMapper carouselMapper;
    @Override
    public PageResult getCarouselPage(PageQueryUtil pageQueryUtil) {
        int totalCount = carouselMapper.getTotalCarouselCount(pageQueryUtil);
        List<Carousel>carousels= carouselMapper.findCarouselList(pageQueryUtil);
        PageResult pageResult = new PageResult(carousels, totalCount, pageQueryUtil.getLimit(), pageQueryUtil.getPage());
        return pageResult;
    }

    @Override
    public String saveCarousel(Carousel carousel) {
        int count= carouselMapper.insertSelective(carousel);
        if(count>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public Carousel getCarouselById(Integer id) {
        return carouselMapper.selectByPrimaryKey(id);
    }

    @Override
    public String updateCarousel(Carousel carousel) {
        Carousel temp = carouselMapper.selectByPrimaryKey(carousel.getCarouselId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        temp.setCarouselUrl(carousel.getCarouselUrl());
        temp.setRedirectUrl(carousel.getRedirectUrl());
        temp.setCarouselRank(carousel.getCarouselRank());
        temp.setUpdateTime(new Date());
        int count = carouselMapper.updateByPrimaryKeySelective(temp);
        if (count > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        } else {
            return ServiceResultEnum.DB_ERROR.getResult();
        }
    }

    @Override
    public String deleteCarousel(Integer[] ids) {
        if (ids.length < 1) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
            }
        int count = carouselMapper.deleteBatch(ids);
        if (count > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();

    }

    @Override
    public List<IndexCarouselVO> getCarouselsForIndex(int number) {
        List<IndexCarouselVO> indexCarouselVOS = new ArrayList<>();
        List<Carousel> carousels = carouselMapper.findCarouselsByNumber(number);
        System.out.println(carousels);
        if (carousels.size() > 0) {
            indexCarouselVOS = BeanUtil.copyList(carousels, IndexCarouselVO.class);

        }
        return indexCarouselVOS;
    }

}
